package com.qipeijia.core.mytest;

import org.apache.ibatis.binding.MapperMethod;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

import java.util.*;
import java.util.Properties;


/**
 * Created by Administrator on 2017/6/27.
 */
@Intercepts({@Signature(
        type= Executor.class,
        method = "query",
        args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})})
public class MySqlPlugin implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        Object[] args=invocation.getArgs();
        MappedStatement ms=(MappedStatement)args[0];
        BoundSql sql=ms.getBoundSql(args[1]);
        Object params=args[1];
        MapperMethod.ParamMap paramMap= (MapperMethod.ParamMap) params;
        List<ParameterMapping> parameterMappings = sql.getParameterMappings();
        System.out.println("sql====>"+sql.getSql());
        for (ParameterMapping mapping:parameterMappings){
            System.out.println(mapping.getProperty()+"====>"+paramMap.get(mapping.getProperty()));
        }

        return invocation.proceed();
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {

    }
}
